package cn.com;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import cn.com.domain.content;
import cn.com.imgloader.ImageLoader;
import cn.com.service.ViewPagerService;
import cn.com.ui.ChartActivity;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
/**
 * 消息
 * @author ChonZY
 */
public class MsgActivity extends  BaseActivity{
	protected static final int FINISH_GET_ADVVIEWPAGE = 0;
	protected static final int ERROR_GET_ADVVIEWPAGE = 1;
	private ViewPager advPager = null;
	private AtomicInteger what = new AtomicInteger(0);
	private boolean isContinue = true;
	public ImageLoader imageLoader;
	private ProgressBar pbForVb ; 
	private TextView tvForvp;
	List<content> mlist;
	
	private ImageView[] imageViews = null;
	private ImageView imageView = null;
	

	
	
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			
			switch (msg.what) {
			case FINISH_GET_ADVVIEWPAGE:
				mlist = (List<content>) msg.obj;
				pbForVb.setVisibility(View.GONE);
				tvForvp.setVisibility(View.GONE);
				initViewPager() ;
				break;
				
			case ERROR_GET_ADVVIEWPAGE:
				pbForVb.setVisibility(View.GONE);
				tvForvp.setVisibility(View.GONE);
				initViewPager();
				
				break ;
			default:
				break;
			}
			
		};
	};
	
	private ListView lv_msgItem ; // 消息列表
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.msg);
		
		init();
		// 顶部广告
		init_topAdvList();
	}
	/**
	 * 测试 聊天界面
	 * @param view
	 */
	public void testUi(View view){
		Intent intent = new Intent(this,ChartActivity.class);
		startActivity(intent);
	}
	
	@Override
	protected void onResume() {
		imageLoader.clearCache();
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		imageLoader.clearCache();
		super.onDestroy();
	}
	private void init_topAdvList() {
		new Thread(){
			
			public void run() {
				try {
					ViewPagerService vps = new ViewPagerService();
					mlist = vps.getContacts("http://192.168.1.103:8080/TaskViewpager/list.xml");
					if(mlist == null || "".equals(mlist)){
						Message msg = new Message();
						msg.what = ERROR_GET_ADVVIEWPAGE;
						mHandler.sendMessage(msg);
					}
					Message msg = new Message();
					msg.what = FINISH_GET_ADVVIEWPAGE;
					msg.obj = mlist ;
					mHandler.sendMessage(msg);
					
				} catch (Exception e) {
					// TODO: handle exception
					Message msg = new Message();
					msg.what = ERROR_GET_ADVVIEWPAGE;
					mHandler.sendMessage(msg);
					
				}
			};
		}.start();
	}

	private void init() {
		initHead();
		tv_centerTop.setText("翼生活");
		imageLoader = new ImageLoader(this);
		pbForVb = (ProgressBar) findViewById(R.id.pb_msg_vpforpb);
		tvForvp = (TextView) findViewById(R.id.tv_msg_vpfortxt);
		tv_rightTop.setVisibility(View.VISIBLE);
		tv_rightTop.setText("注销");
		lv_msgItem = (ListView) findViewById(R.id.lv_msg);
		
	}
	/**
	 * 注销
	 * @param v
	 */
	public void topRight(View v){
		
	}
	
	private void initViewPager(){
		advPager = (ViewPager) findViewById(R.id.vp_msg_adv_pager);
		// 图片列表
		List<View> advPics = new ArrayList<View>();

		if (mlist == null || mlist.size() == 0) {
			ImageView img = new ImageView(this);
			img.setImageDrawable(getResources().getDrawable(
					R.drawable.advertising_default_1));
			advPics.add(img);
		} else {
			
			for (int i = 0; i < mlist.size(); i++) {
				ImageView img = new ImageView(this);
				imageLoader.DisplayImage(mlist.get(i).getImgsrc(),
						MsgActivity.this, img);
				advPics.add(img);
			}
		}
		
		// 设置显示的点
		ViewGroup group = (ViewGroup) findViewById(R.id.vp_msg_viewGroup);
		imageViews = new ImageView[advPics.size()];
		
		for (int i = 0; i < advPics.size(); i++) {
			
			imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(20, 20));
			imageView.setPadding(20, 0, 20, 0);
			imageViews[i] = imageView;
			
			if (i == 0) {
				// 默认选中第一张图片
				imageViews[i]
						.setBackgroundResource(R.drawable.banner_dian_focus);
			} else {
				imageViews[i]
						.setBackgroundResource(R.drawable.banner_dian_blur);
			}
			group.addView(imageViews[i]);
		}
		
		advPager.setAdapter(new AdvAdapter(advPics));
		advPager.setOnPageChangeListener(new GuidePageChangeListener());
		/**
		 * 按下时不继续定时滑动,弹起时继续定时滑动
		 */
		advPager.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_MOVE:
					isContinue = false;
					break;
				case MotionEvent.ACTION_UP:
					isContinue = true;
					break;
				default:
					isContinue = true;
					break;
				}
				return false;
			}
		});
		
		/**
		 * 定时滑动线程
		 */
		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					if (isContinue) {
						viewHandler.sendEmptyMessage(what.get());
						whatOption();
					}
				}
			}

		}).start();
		
		
	}
	
	/**
	 * 操作圆点轮换变背景
	 */
	private void whatOption() {
		what.incrementAndGet();
		if (what.get() > imageViews.length - 1) {
			what.getAndAdd(-6);
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {

		}
	}
	
	/**
	 * 处理定时切换广告栏图片的句柄
	 */
	private final Handler viewHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			advPager.setCurrentItem(msg.what);
			super.handleMessage(msg);
		}

	};
	
	/**
	 * 指引页面改监听器
	 * @author ChonZY
	 *
	 */
	private final class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[arg0]
						.setBackgroundResource(R.drawable.banner_dian_focus);
				if (arg0 != i) {
					imageViews[i]
							.setBackgroundResource(R.drawable.banner_dian_blur);
				}
			}

		}

	}
	
	/**
	 * 广告栏PaperView 图片适配器
	 * @author ChonZY
	 *
	 */
	private final class AdvAdapter extends PagerAdapter{
		private List<View> views = null;

		public AdvAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(views.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(views.get(arg1), 0);
			return views.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

	}

}

